package com.zxc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/19 14:36
  * @description:
  */
@Controller
@RequestMapping("/emp")
public class Demo3Controller {

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/19 15:13
  * @description: 传递参数到前端 方法1
  */
    @RequestMapping("/d1")
    public String d1 (HttpServletRequest request) {
        String name="张三";
        request.setAttribute("name",name);
        return "index";
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/19 15:13
      * @description: 传递参数到前端 方法2
      */
    @RequestMapping("/d2")
    public ModelAndView d2() {
        //创建ModelAndView对象
        ModelAndView mv=new ModelAndView();
        //设置逻辑视图名
        mv.setViewName("index");
        //设置要传递的参数
        mv.addObject("name","李四");
        //返回ModelAndView对象
        return mv;
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/19 15:13
      * @description: 传递参数到前端 方法3
     * Model 接口对象,自动帮我们把数据放到request作用域中
      */
    @RequestMapping("/d3")
    public String d3 (Model model) {
        model.addAttribute("name","王五");
        return "index";
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/19 15:14
      * @description: 传递参数到前端 方法4
     *  MAP集合对象,自动帮我们把数据放到request作用域中
      */
    @RequestMapping("/d4")
    public String d4 (Map<String,Object> maps) {
        maps.put("name","赵六");
        return "index";
    }

    /**
      * @author: zxc
      * @return:
      * @time:  2021/5/19 15:14
      * @description: 传递参数到前端 方法5
     * 重定向 前几种方法都是转发传值
      */
    @RequestMapping("/d5")
    public String d5(HttpSession session) {
        session.setAttribute("name","李二");
        return "demo";
    }
}
